//
//  YGArticleTableViewCell.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/5/3.
//

#import "YGArticleTableViewCell.h"


@interface YGArticleTableViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIImageView *articleImgView;

@end
@implementation YGArticleTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initconstrains];
    }
    return self;
}

-(void)initView{
    [self addSubview:self.titleLabel];
    [self addSubview:self.dateLabel];
    [self addSubview:self.lineView];
    [self addSubview:self.articleImgView];
}

-(void)initconstrains{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_top).offset(15);
            make.left.mas_equalTo(self.mas_left).offset(10);
            make.width.mas_equalTo(240);
            make.height.mas_equalTo(50);
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.titleLabel.mas_left);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(12);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.mas_bottom);
            make.width.mas_equalTo(240);
            make.left.mas_equalTo(self.titleLabel.mas_left);
            make.height.mas_equalTo(0.5);
    }];
    
    [self.articleImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.mas_centerY);
            make.right.mas_equalTo(self.mas_right).offset(-10);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(80);
    }];
}

//数据接口

- (void)configureModel:(NSDictionary *)model{

    if (model) {
       
        _dateLabel.text = [self time_timestampToString:[[model valueForKey:@"createTime"] integerValue]];

        
        NSString *theme = [model objectForKey:@"theme"];
        NSString *title = [theme stringByRemovingPercentEncoding];
        _titleLabel.text = title;
        
        NSString *imgUrl = [model objectForKey:@"fileUrl"];
        NSURL *url = [NSURL URLWithString:imgUrl];
        [_articleImgView sd_setImageWithURL:url];
    }
}

- (NSString *)time_timestampToString:(NSInteger)timestamp{

    timestamp = timestamp/1000.0;
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];

    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];

     [dateFormat setDateFormat:@"yyyy-MM-dd"];

    NSString *string=[dateFormat stringFromDate:confromTimesp];

    return string;

}

#pragma setter
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = UIColorFromRGB(0x333333);
        _titleLabel.font = [UIFont systemFontOfSize:12];
        _titleLabel.numberOfLines = 0;
    }
    return _titleLabel;
}

- (UILabel *)dateLabel{
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc]init];
        _dateLabel.textColor = UIColorFromRGB(0x999999);
        _dateLabel.font = [UIFont systemFontOfSize:12];
    }
    return _dateLabel;
}

- (UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = UIColorFromRGB(0x999999);
    }
    return _lineView;
}

- (UIImageView *)articleImgView{
    if (!_articleImgView) {
        _articleImgView = [[UIImageView alloc]init];
        
    }
    return _articleImgView;
}





@end
